﻿using FlaUI.UIA3;
using System.Diagnostics;
using System;
using System.IO;
using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using System.Linq;
using FlaUI.Core.Definitions;

public class WXWork5PatternDetector
{
    private Window window;

    public bool Initialize()
    {
        try
        {
            System.Threading.Thread.Sleep(8000); // 5.0版本启动较慢

            var processes = Process.GetProcessesByName("WXWork");
            if (processes.Length == 0)
            {
                // 企业微信 5.0 常见安装路径
                string[] paths = {
                    @"D:\Program Files (x86)\WXWork\WXWork.exe",
                    Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\AppData\Local\Programs\WXWork\WXWork.exe")
                };

                foreach (var path in paths)
                {
                    if (File.Exists(path))
                    {
                        var app = Application.Launch(path);
                        System.Threading.Thread.Sleep(8000); // 5.0版本启动较慢
                        break;
                    }
                }
            }

            if (processes.Length > 0)
            {
                var app = Application.Attach(processes[0]);
                using (var automation = new UIA3Automation())
                {
                    window = app.GetMainWindow(automation);
                    return window != null;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"初始化失败: {ex.Message}");
        }
        return false;
    }
    public void DetectWXWork5Patterns()
    {
        Console.WriteLine("=== 企业微信 5.0 模式检测 ===");

        var allElements = window.FindAllDescendants();

        // 检测可输入区域
        var valuePatternElements = allElements.Where(e =>
        {
            try { return e.Patterns.Value.IsSupported; } catch { return false; }
        }).ToList();

        Console.WriteLine($"可输入元素: {valuePatternElements.Count}");
        foreach (var element in valuePatternElements)
        {
            var bounds = element.BoundingRectangle;
            Console.WriteLine($"  输入框 - 位置: ({bounds.Left}, {bounds.Top}), 尺寸: {bounds.Width}x{bounds.Height}");
        }

        // 检测可点击元素
        var invokePatternElements = allElements.Where(e =>
        {
            try { return e.Patterns.Invoke.IsSupported; } catch { return false; }
        }).ToList();

        Console.WriteLine($"\n可点击元素: {invokePatternElements.Count}");

        // 按位置分组显示
        var leftSide = invokePatternElements.Where(e => e.BoundingRectangle.Left < 100).ToList();
        var topSide = invokePatternElements.Where(e => e.BoundingRectangle.Top < 50).ToList();
        var rightSide = invokePatternElements.Where(e => e.BoundingRectangle.Left > window.BoundingRectangle.Width - 100).ToList();
        var bottomSide = invokePatternElements.Where(e => e.BoundingRectangle.Top > window.BoundingRectangle.Height - 50).ToList();

        Console.WriteLine($"左侧元素: {leftSide.Count}");
        Console.WriteLine($"顶部元素: {topSide.Count}");
        Console.WriteLine($"右侧元素: {rightSide.Count}");
        Console.WriteLine($"底部元素: {bottomSide.Count}");

        // 检测列表/树形控件
        var selectionPatternElements = allElements.Where(e =>
        {
            try { return e.Patterns.Selection.IsSupported; } catch { return false; }
        }).ToList();

        Console.WriteLine($"\n可选择元素: {selectionPatternElements.Count}");
        foreach (var element in selectionPatternElements)
        {
            var bounds = element.BoundingRectangle;
            Console.WriteLine($"  列表 - 位置: ({bounds.Left}, {bounds.Top}), 尺寸: {bounds.Width}x{bounds.Height}");
        }
    }
}